
bt.module({builder: function(bt) {
    var j$ = jQuery;
    var map;
    var currentMarker;

    var updateMarkerLocation = function() {
        var latlng = currentMarker.getPosition();
        $('#latitude').val(latlng.lat());
        $('#longitude').val(latlng.lng());
    };

    var placeMarker = function(map, lat, lng) {
        if (currentMarker) {
            currentMarker.setMap(null);
        }

        currentMarker= new google.maps.Marker({
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: new google.maps.LatLng(lat, lng), 
            map: map
        });

        updateMarkerLocation();
        google.maps.event.addListener(currentMarker, 'dragend', updateMarkerLocation);
    };

    var initialize = function() {
        var options = {
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map"), options);

        var lat = j$('#map').data('lat');
        var lng = j$('#map').data('lng');
        if (lat && lng) {
            lat = parseFloat(lat);
            lng = parseFloat(lng);

            var latlng = new google.maps.LatLng(lat, lng);
            map.setCenter(latlng);
            map.setZoom(14);
            currentMarker= new google.maps.Marker({
                draggable: true,
                animation: google.maps.Animation.DROP,
                position: latlng,
                map: map
            });
            updateMarkerLocation();
            google.maps.event.addListener(currentMarker, 'dragend', updateMarkerLocation);
        } else {
            var latlngbounds = new google.maps.LatLngBounds();
            latlngbounds.extend(new google.maps.LatLng(70, -180));
            latlngbounds.extend(new google.maps.LatLng(-30, 180));
            map.fitBounds(latlngbounds);
        }

        google.maps.event.addListener(map, 'click', function(event) {
            placeMarker(map, event.latLng.lat(), event.latLng.lng());
        });
        
        geocoder = new google.maps.Geocoder();
    };

    var searchAddress = function() {
        j$('.pos-search input').blur();
        var address = j$('.pos-search input').val();
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                if (map.getZoom() < 14)
                    map.setZoom(14);
            } else {
                alert("未找到相关位置，修改搜索条件再试试？");
            }
        });
    };

    j$(document).ready(function() {
        bt.gmap.initMapScript(initialize);
        bt.misc.initDatepicker();

        var curr_cover = j$(".photo-cover").css("background-image");
        if(curr_cover) {
            curr_cover = curr_cover.substring(4);
            curr_cover = curr_cover.substring(0, curr_cover.length - 1);
            j$("#photo-list-popup .photo-item img[src='" + curr_cover + "']")
                    .parents(".photo-item").addClass("selected");
        }

        j$(".photo-cover, .change-photo-btn").click(function() {
            var popup = $("#photo-list-popup");
            var overlay = $("#overlay");
            popup.fadeIn();
            overlay.show();
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;
            var popupHeight = popup.height();
            var popupWidth = popup.width();
            popup.css({
                "position": "fixed",
                "top": windowHeight / 2 - popupHeight / 2,
                "left": windowWidth / 2 - popupWidth / 2
            });
            overlay.click(function() {
                popup.fadeOut();
                overlay.hide();
            });
            j$("#photo-list-popup .close").click(function() {
                popup.fadeOut();
                overlay.hide();
            });

            j$(".photo-item img.photo").each(function(){
                bt.misc.centerAlignImg($(this), 132, 132);
            });

        });

        j$(".photo-item").click(function() {
            j$("#photo-list-popup .selected").removeClass("selected");
            j$(this).addClass("selected");
            j$("input.cover-image").val(j$(this).data("waypoint-id"));
            j$(".photo-cover").css("background-image", "url('" + j$(this).find("img.photo").attr("src") + "')");
            j$("#photo-list-popup .close").click();
        });

        // Search position
        j$(".pos-search .search-btn").click(function() {
            searchAddress();
            return false;
        });

        j$('.pos-search input').keydown(function(e) {
            var code = e.which;
            if (code == 13) {
                searchAddress();
                e.preventDefault();
            }
        });

        j$(".done").click(function() {
            j$("form#edit-form").submit();
        });
        j$(".edit-trip-info .button .close").click(function() {
            var rtn = confirm("确定要结束吗？");
            if (rtn) {
                j$("form#edit-form").append("<input type='hidden' name='close_trip' value='1' />");
                j$("form#edit-form").submit();
            }
        });
    });
}});
